<?php
/**
 * Created by PhpStorm.
 * User: liuchaozhong
 * Date: 2018/8/24
 * Time: 上午10:54
 */
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ViewLog extends Model
{
    protected $table = 'view_log';
    public $timestamps = false;
    public $fillable = ['member_id','article_id','date','nums'];

    //关联文章
    public function g_article()
    {
        return $this->belongsTo(Article::class, 'article_id');
    }

    //添加观看记录
    public function addView($member_id, $article_id)
    {
        $today = date('Y-m-d');
        $view_info = ViewLog::where('member_id',$member_id)
            ->where('article_id', $article_id)
            ->where('date', $today)
            ->first();
        if(!empty($view_info)) {
            $view_info->nums += 1;
            $res = $view_info->save();

            return $res ? 'update' : false;
        }else {
            $insert = [
                'member_id' => $member_id,
                'article_id' => $article_id,
                'date' => $today,
                'nums' => 1
            ];
            $res = ViewLog::create($insert);
        }

        return $res ? 'insert' : false;
    }

}